
package com.example.cameraproject

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
    private val CAMERA_PERMISSION_REQUEST_CODE = 8888
    private val CAMERA_REQUEST_CODE = 9999

    private lateinit var takePhotoButton: Button // 声明拍照按钮
    private lateinit var chooseFromAlbumButton: Button // 声明从相册选择按钮
    private lateinit var pictureImageView: ImageView // 声明图片显示控件

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        takePhotoButton = findViewById(R.id.take_photo) // 获取拍照按钮
        chooseFromAlbumButton = findViewById(R.id.choose_from_album) // 获取从相册选择按钮
        pictureImageView = findViewById(R.id.picture) // 获取图片显示控件

        takePhotoButton.setOnClickListener { // 设置拍照按钮点击事件
            if (ContextCompat.checkSelfPermission(
                    this@MainActivity,
                    Manifest.permission.CAMERA
                ) != PackageManager.PERMISSION_GRANTED // 判断是否有相机权限
            ) {
                ActivityCompat.requestPermissions( // 请求相机权限
                    this@MainActivity,
                    arrayOf(Manifest.permission.CAMERA),
                    CAMERA_PERMISSION_REQUEST_CODE
                )
            } else {
                startCamera() // 启动相机
            }
        }

        chooseFromAlbumButton.setOnClickListener { // 设置从相册选择按钮点击事件
            val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) // 创建选择图片的Intent
            startActivityForResult(intent, CAMERA_REQUEST_CODE) // 启动选择图片的Activity
        }
    }

    private fun startCamera() { // 启动相机
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) // 创建启动相机的Intent
        startActivityForResult(intent, CAMERA_REQUEST_CODE) // 启动相机
    }

    override fun onRequestPermissionsResult( // 处理权限请求结果
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            startCamera() // 启动相机
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { // 处理Activity返回结果
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == Activity.RESULT_OK && (requestCode == CAMERA_REQUEST_CODE || requestCode == Activity.RESULT_OK)) {
            val thumbnail: Bitmap? = data?.extras?.get("data") as Bitmap? // 获取拍照返回的缩略图
            pictureImageView.setImageBitmap(thumbnail) // 显示缩略图
        }
    }
}

